/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "player.h"

#include <QObject>
#include <QStringList>
#include <QUrl>

#include <phonon/MediaSource>
#include <phonon/MediaObject>
#include <phonon/BackendCapabilities>

#include <QDebug>

StreamPlayer :: StreamPlayer(/*const QUrl& streamLink, */QObject* parent/*=0*/)
    :QObject(parent)
{
    pStreamObject = new Phonon::MediaObject(this);
    pStreamOutput = new Phonon::AudioOutput(Phonon::MusicCategory,this);

    audioPath = Phonon::createPath(pStreamObject,pStreamOutput);
    // Since the play is started by the main window ... we don't need it here anymore
    // pStreamObject->play();

    connect(pStreamObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(playerStateChanged(Phonon::State)));

    qDebug() << "StreamPlayer constructed "/* << streamLink*/;
    qDebug() << "Effects available" << Phonon::BackendCapabilities::availableAudioEffects();
}

StreamPlayer :: ~StreamPlayer(void)
{
    delete pStreamOutput;
    delete pStreamObject;

    qDebug() << "StreamPlayer deleted";
}

void StreamPlayer :: setSource(const QUrl& streamLink)
{
    qDebug() << "StreamPlayer :: setSource: " << streamLink;

    pStreamObject->clearQueue();

    pStreamObject->setCurrentSource(Phonon::MediaSource(streamLink));
}

void StreamPlayer :: playerStateChanged(Phonon::State newstate)
{
    qDebug() << "StreamPlayer :: playerStateChanged (" << newstate << ")";

    if ( newstate == Phonon::ErrorState )
    {
        qDebug() << "Error Type: " << pStreamObject->errorType() << " Error: " << pStreamObject->errorString();
        emit errorOccured(pStreamObject->errorString());
    }
    else if ( newstate == Phonon::PlayingState )
    {
        // Playing state -> sending message to say that we can get the metadata"
        emit playingStateSignal();
    }
}

void StreamPlayer :: play()
{
    qDebug() << "StreamPlayer :: play";

    pStreamObject->play();
}

void StreamPlayer :: pause()
{
    qDebug() << "StreamPlayer :: pause";

    pStreamObject->pause();
}

void StreamPlayer :: stop()
{
    qDebug() << "StreamPlayer :: stop";

    pStreamObject->stop();
}

void StreamPlayer :: decreaseVolume(void)
{
    qDebug() << "StreamPlayer :: decreaseVolume() (Was: " << pStreamOutput->volume() << " )";
    if ( pStreamOutput->volume() > 0.01 )
    {
        pStreamOutput->setVolume(pStreamOutput->volume()-0.01);
    }
}

void StreamPlayer :: increaseVolume(void)
{
    qDebug() << "StreamPlayer :: increaseVolume() (Was: " << pStreamOutput->volume() << " )";
    if ( pStreamOutput->volume() < 1 )
    {
        pStreamOutput->setVolume(pStreamOutput->volume()+0.01);
    }
}

void StreamPlayer :: switchMuteVolume(void)
{
    qDebug() << "StreamPlayer :: switchMuteVolume() (Was: " << pStreamOutput->isMuted() << " )";

    pStreamOutput->setMuted(!pStreamOutput->isMuted());
}
